<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Import extends zulController {
	function __construct(){
		parent::__construct();
		$this->load->library("form_validation");
		$this->load->model("master_mahasiswa_model");
		$this->load->model("master_tahun_ajaran_model");
		$this->load->model("master_kelas_model");
		$this->load->model("master_matkul_model");
		$this->load->model("nilai_mahasiswa_model");
		$this->load->model("semester_mahasiswa_model");
		$this->load->model("absensi_mahasiswa_model");
		$this->checkLogin();
	}
	public function index()
	{
		$this->form_validation->set_rules("pilihan","Pilihan Import","required");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in">', '</div>');
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			$data["active"] = "data";
			$data["tahun"]	= $this->master_tahun_ajaran_model->get();
			$data["content"]	= $this->load->view("form_import",$data,true);
			$this->load->view('main_new',$data);
		} else {
			error_reporting(E_ALL ^ E_NOTICE);
			error_reporting(0);
			require_once 'include/excel_reader.php';
			$pilihan	= $this->input->post("pilihan");
			$data = new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
			$count = 0;
			if($pilihan == 1){
				$join = array(
						array("table"=>"master_kelas","join_key"=>"ID_JURUSAN","join_table"=>"master_jurusan"),
						array("table"=>"master_kelas","join_key"=>"ID_PROGRAM","join_table"=>"master_program"));
				foreach($data->dump(false,false,false,false) as $row){
					if(count($this->master_mahasiswa_model->get_by(array("NRP"=>$row["NRP"]))) == 0){
						$row["TGLMASUK"]	= ($row["TGLMASUK"] == NULL || $row["TGLMASUK"] == "" || $row["TGLMASUK"] == "0000-00-00") ? (NULL) : date("Y-m-d",strtotime($row["TGLMASUK"]));
						$row["TGLLULUS"]	= ($row["TGLLULUS"] == NULL || $row["TGLLULUS"] == "" || $row["TGLLULUS"] == "0000-00-00") ? (NULL) : date("Y-m-d",strtotime($row["TGLLULUS"]));
						$row["TGLLAHIR"]	= ($row["TGLLAHIR"] == NULL || $row["TGLLAHIR"] == "" || $row["TGLLAHIR"] == "0000-00-00") ? (NULL) : date("Y-m-d",strtotime($row["TGLLAHIR"]));
						$data	= $this->master_kelas_model->get_join_by($join,array("NAMA_PROGRAM"=>$row["PROGRAM"],"NAMA_JURUSAN"=>$row["JURUSAN"],"NAMA_KELAS"=>$row["PARAREL"]),TRUE);
						$row["ID_KELAS"] = $data["ID_KELAS"];
						$row["STATUS"]	= 0;
						if(is_numeric($row["NRP"]))
						$result = $this->master_mahasiswa_model->save($row);
					}
					if($result != NULL || $result != ""){
						$count++;
					}
				}
				if($count > 0){
					?>
					<script language="javascript">
						alert("Data berhasil disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?php
				} else {
					?>
					<script language="javascript">
						alert("Data gagal disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?
				}
			} else if($pilihan == 2) {
				$tahun	= $this->input->post("tahun");
				$semester	= $this->input->post("semester");
				foreach($data->dump(false,false,false,false) as $row){
					if(($row["NRP"] != NULL || $row["NRP"] != "") && is_numeric($row["NRP"])){
						$value	= $this->master_mahasiswa_model->get_by(array("NRP"=>$row["NRP"]),FALSE,FALSE,TRUE);
						while ($col = current($row)) {
							if(key($row) != "NO" && key($row) != "NRP" && key($row) != "NAMA" && key($row) != "IPS" && key($row) != "STATUS" && key($row) != "J.PENERIMAAN"){
								$value2	= $this->master_matkul_model->get_by(array("NAMA_MATA_KULIAH"=>key($row)),FALSE,FALSE,TRUE);
								$data	= array("ID_MATA_KULIAH"=>$value2["ID_MATA_KULIAH"],"ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"NILAI"=>$row[key($row)],"SEMESTER"=>$semester);
								if(count($this->nilai_mahasiswa_model->get_by(array("ID_MATA_KULIAH"=>$value2["ID_MATA_KULIAH"],"ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"SEMESTER"=>$semester))) < 1){
									$this->nilai_mahasiswa_model->save($data);
								}
							}
							next($row);
						}
						$data	= array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"IPS"=>$row["IPS"],"STATUS"=>(($row["STATUS"] == "Naik") ? (1) : (0)),"SEMESTER"=>$semester);
						if(count($this->semester_mahasiswa_model->get_by(array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"SEMESTER"=>$semester))) < 1){
							$result	= $this->semester_mahasiswa_model->save($data);
							if($result != NULL || $result != ""){
								$count++;
							}
						}
					}
				}
				if($count > 0){
					?>
					<script language="javascript">
						alert("Data berhasil disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?php
				} else {
					?>
					<script language="javascript">
						alert("Data gagal disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?
				}
			} else if($pilihan == 3) {
				$tahun	= $this->input->post("tahun");
				$semester	= $this->input->post("semester");
				foreach($data->dump(false,false,false,false) as $row){
					if(($row["NRP"] != NULL || $row["NRP"] != "") && is_numeric($row["NRP"])){
						$value	= $this->master_mahasiswa_model->get_by(array("NRP"=>$row["NRP"]),FALSE,FALSE,TRUE);
						/* while ($col = current($row)) {
							if(key($row) != "NO" && key($row) != "NRP" && key($row) != "NAMA"){
								// $value2	= $this->master_matkul_model->get_by(array("NAMA_MATA_KULIAH"=>key($row)),FALSE,FALSE,TRUE);
								$data2	= array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"MINGGU_KE"=>key($row),"TOTAL_ABSEN"=>$row[key($row)],"SEMESTER"=>$semester);
								// print_r($data2);
								// echo "<br>";
								// if(count($this->absensi_mahasiswa_model->get_by(array("MINGGU_KE"=>key($row),"ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"SEMESTER"=>$semester))) < 1){
									// $this->absensi_mahasiswa_model->save($data);
									// $count++;
								// }
								// echo count($row[12]);
								// echo $row[2];
								// echo $row[3];
								// echo $row[4];
								// echo "<br>";
							}
							// print_r($row);
							// echo "<br>";
							next($row);
						} */
						foreach($row as $col=>$val){
							if($col != "NO" && $col != "NRP" && $col != "NAMA"){
								$data2	= array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"MINGGU_KE"=>$col,"TOTAL_ABSEN"=>$val,"SEMESTER"=>$semester);
								$this->absensi_mahasiswa_model->save($data2);
								$count++;
							}
						}
					}
				}
				if($count > 0){
					?>
					<script language="javascript">
						alert("Data berhasil disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?php
				} else {
					?>
					<script language="javascript">
						alert("Data gagal disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?
				}
			} else {
				$tahun	= $this->input->post("tahun");
				$semester	= $this->input->post("semester");
				foreach($data->dump(false,false,false,false) as $row){
					if(($row["NRP"] != NULL || $row["NRP"] != "") && is_numeric($row["NRP"])){
						$value	= $this->master_mahasiswa_model->get_by(array("NRP"=>$row["NRP"]),FALSE,FALSE,TRUE);
						while ($col = current($row)) {
							if(key($row) != "NO" && key($row) != "NRP" && key($row) != "NAMA" && key($row) != "IPS" && key($row) != "STATUS" && key($row) != "J.PENERIMAAN"){
								$value2	= $this->master_matkul_model->get_by(array("NAMA_MATA_KULIAH"=>key($row)),FALSE,FALSE,TRUE);
								$data	= array("ID_MATA_KULIAH"=>$value2["ID_MATA_KULIAH"],"ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"NILAI"=>$row[key($row)],"SEMESTER"=>$semester);
								if(count($this->nilai_mahasiswa_model->get_by(array("ID_MATA_KULIAH"=>$value2["ID_MATA_KULIAH"],"ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"SEMESTER"=>$semester))) < 1){
									$this->nilai_mahasiswa_model->save($data);
								}
							}
							next($row);
						}
						$data	= array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"IPS"=>$row["IPS"],"STATUS"=>(($row["STATUS"] == "Naik") ? (1) : (0)),"SEMESTER"=>$semester);
						if(count($this->semester_mahasiswa_model->get_by(array("ID_MAHASISWA"=>$value["ID_MAHASISWA"],"ID_TAHUN_AJARAN"=>$tahun,"SEMESTER"=>$semester))) < 1){
							$result	= $this->semester_mahasiswa_model->save($data);
							if($result != NULL || $result != ""){
								$count++;
							}
						}
					}
				}
				if($count > 0){
					?>
					<script language="javascript">
						alert("Data berhasil disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?php
				} else {
					?>
					<script language="javascript">
						alert("Data gagal disimpan");
						document.location="<?php echo site_url("import"); ?>";
					</script>
					<?
				}
			}
		}
	}
}